package com.alks.function.controller.common;

import com.alks.common.aop.ApiRestController;
import com.alks.entity.data.page.PageParam;
import com.alks.entity.data.page.PageRecord;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.back.MtlSubRequest;
import com.alks.function.data.request.procurement.SdVendorRequest;
import com.alks.function.data.request.stockdept.pcfactorystockwork.LotComMtlRequest;
import com.alks.function.mapper.common.BackMapper;
import com.alks.function.service.common.BackService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;

@RestController
@ApiRestController("/back")
@Api(value = "回显总查询", tags = "回显总查询")
@Slf4j
public class BackController {
    @Autowired
    private BackService backService;

    @GetMapping("/vendorQuery")
    @ApiOperation(value = "厂商-全部查询", tags = "回显总查询")
    public ResponseInfo vendorQuery() {
        return backService.vendorQuery();
    }

    @GetMapping("/vendorAllQuery")
    @ApiOperation(value = "厂商-全部查询（查询本公司的厂商和其他公司有而自己公司没有的厂商）", tags = "回显总查询")
    public ResponseInfo vendorAllQuery() {
        return backService.vendorAllQuery();
    }

    @GetMapping("/vendorByCI")
    @ApiOperation(value = "厂商-根据公司ID查名称", tags = "回显总查询")
    public ResponseInfo vendorByCI(){
        return backService.vendorByCI();
    }

    @PostMapping("/getVendorByCondition")
    @ApiOperation(value = "根据条件查询所有厂商信息", tags = "回显总查询")
    public ResponseInfo getVendorByCondition(@RequestBody SdVendorRequest request) {
        return backService.getVendorByCondition(request);
    }

    @GetMapping("/getVendorNoByName")
    @ApiOperation(value = "厂商-根据ID查名称", tags = "回显总查询")
    public ResponseInfo getVendorNoByName(@RequestParam @ApiParam("厂商ID") String vendorNo) {
        return ResponseInfo.ok(backService.getVendorNoByName(vendorNo));
    }

    @GetMapping("/mtlTypeQuery")
    @ApiOperation(value = "物料类型-全部查询", tags = "回显总查询")
    public ResponseInfo mtlTypeQuery() {
        return backService.mtlTypeQuery();
    }

    @GetMapping("/deptQuery")
    @ApiOperation(value = "部门-全部查询", tags = "回显总查询")
    public ResponseInfo deptQuery() {
        return backService.deptQuery();
    }

    @GetMapping("/inSlipReasonQuery")
    @ApiOperation(value = "入库原因-全部查询", tags = "回显总查询")
    public ResponseInfo inSlipReasonQuery() {
        return backService.inSlipReasonQuery();
    }

    @GetMapping("/outSlipReasonQuery")
    @ApiOperation(value = "出库原因-全部查询", tags = "回显总查询")
    public ResponseInfo outSlipReasonQuery() {
        return backService.outSlipReasonQuery();
    }

    @GetMapping("/mtlTypeLMSQuery")
    @ApiOperation(value = "物料类型-多级查询（大类 中类 小类）", tags = "回显总查询")
    public ResponseInfo mtlTypeLMSQuery(@RequestParam(required = false) @ApiParam("上一级物料代码（没有则不写）") String mtlType) {
        return backService.mtlTypeLMSQuery(mtlType);
    }

    @GetMapping("/order/allOrderCondition")
    @ApiOperation(value = "大底、楦头、季度、样品类型、品牌", tags = "回显总查询")
    public ResponseInfo getAllOrderCondition() {
        return backService.getAllOrderCondition();
    }

    @GetMapping("/duty/getDutyCondition")
    @ApiOperation(value = "责任人、责任原因")
    public ResponseInfo getDutyCondition() {
        return backService.getDutyCondition();
    }

    @GetMapping("/getMtlType3")
    @ApiOperation(value = "材料分类名称-三级材料编码")
    public ResponseInfo getMtlType3() {
        return backService.getMtlType3();
    }

    @GetMapping("/getYfMtlType3")
    @ApiOperation(value = "材料分类名称-三级材料编码yf")
    public ResponseInfo getYfMtlType3() {
        return backService.getYfMtlType3();
    }
    //材料名称返回数据
    @GetMapping("/getMtlName3")
    @ApiOperation(value = "材料分类名称3查询材料名称的基本信息")
    public ResponseInfo getMtlName3(@RequestParam(required = false) String mtlName, @RequestParam(required = false) String mtlNo, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        return backService.getMtlName3(mtlName, mtlNo, pageNum, pageSize);
    }

    @GetMapping("/getMtlUS1")
    @ApiOperation("获取材料规格和单位")
    //获取材料规格和单位
    public ResponseInfo getMtlUS1() {
        return backService.getMtlUS1();
    }

    @PutMapping("/getEmployee")
    @ApiOperation("员工信息-全部查询")
    //获取材料规格和单位
    public ResponseInfo getEmployee(@RequestBody PageParam request,
                                    @RequestParam(required = false, value = "deptName") @ApiParam("部门名称") String deptName,
                                    @RequestParam(required = false, value = "name") @ApiParam("员工名称") String name) {
        return backService.getEmployee(request, deptName, name);
    }

    //大底的厂商单独写个返回
    @GetMapping("/vendorMoldQuery")
    @ApiOperation(value = "厂商-大底厂商", tags = "回显总查询")
    public ResponseInfo vendorMoldQuery() {
        return backService.vendorMoldQuery();
    }

    //库存位置建档的返回条件
    @GetMapping("/stockArea")
    @ApiOperation(value = "库存位置", tags = "回显总查询")
    public ResponseInfo stockArea() {
        return backService.stockArea();
    }

    @GetMapping("/getVendorCompanyName")
    @ApiOperation(value = "厂商公司名称", tags = "回显总查询")
    public ResponseInfo getVendorCompanyName() {
        return backService.getVendorCompanyName();
    }

    @GetMapping("/getVendorCompanyNameByCompanyId")
    @ApiOperation(value = "厂商公司名称-通过id找", tags = "回显总查询")
    public ResponseInfo getVendorCompanyNameByCompanyId(@RequestParam String companyId) {
        return backService.getVendorCompanyNameByCompanyId(companyId);
    }

    @GetMapping("/getLots")
    @ApiOperation(value = "指令号", tags = "回显总查询")
    public ResponseInfo getLots(@RequestParam(required = false) String lot, @RequestParam Integer pageSize, @RequestParam Integer pageNum) {
        return backService.getLots(lot, pageNum, pageSize);
    }

    @PutMapping("/getRyLots")
    @ApiOperation(value = "荣阳指令号", tags = "回显总查询")
    public ResponseInfo getRyLots(@RequestParam(required = false) String lotNo,
                                @RequestBody PageParam request) {
        List<String> lotNoList = backService.getRyLots(lotNo,request);
        return ResponseInfo.ok(lotNoList);
    }

    @GetMapping("/getRyLotCheck")
    @ApiOperation(value = "检测是否为荣阳指令号（不是则抛异常）", tags = "回显总查询")
    public ResponseInfo getRyLotCheck(@RequestParam(required = false) String lotNo) {
        backService.getRyLotCheck(lotNo);
        return ResponseInfo.ok();
    }

    @PutMapping("/getVendorQuery")
    @ApiOperation(value = "厂商-查询（昊文）", tags = "回显总查询")
    public ResponseInfo getVendorQuery(@RequestParam(required = false) @ApiParam("厂商ID") String vendorNo,
                                       @RequestParam(required = false) @ApiParam("厂商名称") String vendorName,
                                       @RequestBody PageParam request) {
        return backService.getVendorQuery(vendorNo, vendorName, request);
    }

    @PutMapping("/getVendorSearch")
    @ApiOperation(value = "厂商整合查询（昊文）", tags = "回显总查询")
    public ResponseInfo getVendorSearch(@RequestParam(required = false) @ApiParam("查询条件") String search,
                                       @RequestBody PageParam request) {
        PageRecord<Map<String,String>> record =  backService.getVendorSearch(search, request);
        return ResponseInfo.ok(record);
    }

    @PutMapping("/getMtlSub")
    @ApiOperation("材料库存-条件查询（昊文）")
    //获取材料规格和单位
    public ResponseInfo getMtlSub(@RequestBody MtlSubRequest request) {
        return backService.getMtlSub(request);
    }

    @PostMapping("/getMtlType")
    @ApiOperation("材料类型-整合查询（昊文）")
    //获取材料规格和单位
    public ResponseInfo getMtlType(@RequestParam(required = false) @ApiParam("种类级别（有1-3级）") Integer levelCode,
                                   @RequestParam(required = false) @ApiParam("所属分类（用于查询所属子类）") String typeNo) {
        return backService.getMtlType(levelCode, typeNo);
    }

    @GetMapping("/getChangeOfOrderMakerList")
    @ApiOperation("研发型体建档--制单员")
    public ResponseInfo getChangeOfOrderMakerList() {
        return backService.getChangeOfOrderMakerList();
    }

    @GetMapping("/getSizeTypeByName")
    @ApiOperation("研发型体建档--码制")
    public ResponseInfo getSizeTypeByName() {
        return backService.getSizeTypeByName();
    }

    @GetMapping("/getSlipType")
    @ApiOperation("补制类型-回显查询（昊文）")
    public ResponseInfo getSlipType() {
        return backService.getSlipType();
    }

    @GetMapping("/getStitchLine")
    @ApiOperation("针车组别")
    public ResponseInfo getStitchLine() {
        return backService.getStitchLine();
    }

    @PostMapping("/getStyleQuery")
    @ApiOperation("型体-回显查询（昊文）")
    public ResponseInfo getStyleQuery(@RequestParam(required = false) @ApiParam("型体") String style) {
        return backService.getStyleQuery(style);
    }

    @PostMapping("/getSlReasonQuery")
    @ApiOperation("剩料出入库原因-查询（昊文）")
    public ResponseInfo getSlReasonQuery(@RequestParam @ApiParam("I为入库原因 O为出库原因") String type) {
        return backService.getSlReasonQuery(type);
    }

    @PostMapping("/getMtrReasonQuery")
    @ApiOperation("耗品出入库原因-查询（昊文）")
    public ResponseInfo getMtrReasonQuery(@RequestParam @ApiParam("I为入库原因 O为出库原因") String type) {
        return backService.getMtrReasonQuery(type);
    }

    @GetMapping("/getMtrTypeQuery")
    @ApiOperation("耗品分类查询-查询（昊文）")
    public ResponseInfo getMtrTypeQuery() {
        return backService.getMtrTypeQuery();
    }

    @PutMapping("/getMtrQuery")
    @ApiOperation("耗品材料查询-查询（昊文）")
    public ResponseInfo getMtrQuery(@RequestBody PageParam request,
                                    @RequestParam(required = false) @ApiParam("物料名称") String mtlName,
                                    @RequestParam(required = false) @ApiParam("分类ID") String type) {
        return backService.getMtrQuery(type, mtlName,request);
    }

    @PostMapping("/getStcColQuery")
    @ApiOperation("缴库部门查询-可条件查询（昊文）")
    public ResponseInfo getStcColQuery(@RequestParam(required = false)@ApiParam("1.缴库  2.外发")Integer type) {
        return backService.getStcColQuery(type);
    }

    @GetMapping("/getLineColQuery")
    @ApiOperation("领用单位查询-可条件查询（昊文）")
    public ResponseInfo getLineColQuery() {
        return backService.getLineColQuery();
    }

    @PutMapping("/getComMtlQuery")
    @ApiOperation("通用材料-分页可条件查询（昊文）")
    public ResponseInfo getComMtlQuery(@RequestBody LotComMtlRequest request) {
        return backService.getComMtlQuery(request);
    }

    @PostMapping("/getBaseDetailQuery")
    @ApiOperation("基本资料查询-条件查询（昊文）")
    public ResponseInfo getBaseDetailQuery(@RequestParam(required = false)@ApiParam("类型代码(无入参会返回类型)") String baseType,
                                           @RequestParam(required = false)@ApiParam("关键字筛选（例如入库 出库）")String itemName) {
        return backService.getBaseDetailQuery(baseType,itemName);
    }

    @PostMapping("/getLastTempQuery")
    @ApiOperation("楦头回显-查询（昊文）")
    public ResponseInfo getLastTempQuery(@RequestParam(required = false)@ApiParam("关键字搜索") String lastNo) {
        return backService.getLastTempQuery(lastNo);
    }

    @PostMapping("/getMaterialType")
    @ApiOperation("材料类型回显-查询")
    public ResponseInfo getMaterialType(@RequestParam(required = false) String typeNo) {
        return backService.getMaterialType(typeNo);
    }

    @GetMapping("/getLotNoAndStyle")
    @ApiOperation("指令和型体回显-查询")
    public ResponseInfo getLotNoAndStyle(@RequestParam String lotNo) {
        return backService.getLotNoAndStyle(lotNo);
    }

    @GetMapping("/getLotNo")
    @ApiOperation("指令回显以20分组-查询")
    public ResponseInfo getLotNo(@RequestParam(required = false) String lotNo, Integer pageSize, Integer pageNum) {
        return backService.getLotNo(lotNo, pageSize, pageNum);
    }

    @PostMapping("/getStitchEmployee")
    @ApiOperation("针车员工回显-查询")
    public ResponseInfo getStitchEmployee(@RequestBody PageParam request,
                                          @RequestParam(required = false) @ApiParam("员工名称") String workName,
                                          @RequestParam(required = false) @ApiParam("员工ID") String workId) {
        return backService.getStitchEmployee(request, workName, workId );
    }

    @PostMapping("/getStitchpeProcess")
    @ApiOperation("针车工序回显-查询")
    public ResponseInfo getStitchpeProcess(@RequestBody PageParam request,
                                           @RequestParam(required = false) @ApiParam("工序") String process) {
        return backService.getStitchpeProcess(request,process);
    }

    @PostMapping("/getStitchpeDept")
    @ApiOperation("针车组别回显-查询")
    public ResponseInfo getStitchpeDept(@RequestBody PageParam request,
                                        @RequestParam(required = false) @ApiParam("组名") String deptName,
                                        @RequestParam(required = false) @ApiParam("ID") String deptNo) {
        return backService.getStitchpeDept(request,deptName,deptNo);
    }

    @GetMapping("/getPcCostType")
    @ApiOperation(value = "费用类型查询", tags = "回显总查询")
    public ResponseInfo getPcCostType(@RequestParam(required = false) String typeNo,@RequestParam(required = false) String typeName) {
        return backService.getPcCostType(typeNo,typeName);
    }

    @PostMapping("/getOrderId")
    @ApiOperation(value = "研发指令查询", tags = "回显总查询")
    public ResponseInfo getOrderId(@RequestBody PageParam request,
                                   @RequestParam(required = false) String search) {
        return ResponseInfo.ok(backService.getOrderId(request,search));
    }


    @Autowired
    BackMapper backMapper;
    @GetMapping("/setUrl")
    @Transactional
    public void setUrl() throws IOException {
        List<String> list = backMapper.getUrl();
        int i = 0;
        for (String string : list) {
            i++;
            log.info("这是第"+i+"条");
            if (string != null){
            URL url = new URL(string);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            if (connection.getResponseCode()!=200){
                log.info(string);
                backMapper.deleteUrl(string);
                //                throw new IOException();
            }}
        }
    }


}
